<?php


namespace app\miniweb\controller\v2;


use app\common\cachemodel\MiniChannel;
use app\common\lib\exception\ApiException;
use app\common\lib\Util;
use app\miniweb\controller\Auth;
use think\Env;
use think\Log;
use function app\miniapp\isShowDownloadBar;

class NatalReport extends Auth
{
    public function _initialize($headerInfo = [])
    {
        $accessToken = input('access_token/s', '');
        $this->headers = ['access-token' => $accessToken];
        $this->assign('version', true);
        parent::_initialize();
    }

    protected $type = [
        39 => 'feelings', // 恋爱
        40 => 'dating', // 婚姻
        41 => 'money', // 财富
        42 => 'carrer', // 事业
        52 => 'academic', // 学业
    ];
    protected $recodeInfo = [];

    public function index()
    {
        $height = input('h/d', 0);
        $gid = input('gid/d', 0);
        $rid = input('rid/d', 0);
        $channel = input('channel/d', 1);
        $channel = $channel === 0 ? 1 : $channel;//在前端丢失channelId的情况下，默认为1（准了星盘默认渠道）
        $callback_url = input('callback_url/s');
        $astro_home_page = Env::get('miniapp.astro_home_page');
        $userInfo = $this->user;
        $oss = Env::get('host.photo');
        if (!isset($this->type[$gid]))
        {
            Log::write(date('Y-m-d H:i:s') . 'gid参数错误:' . $gid, 'error', true);
            throw new ApiException('参数错误');
        }

        $appType = $this->request->header('app-type');

        $natalReportData = (new \app\web\service\NatalReport())->getNatalReport((int)$userInfo['id'], $gid, $rid);

        $levels = $natalReportData['level'];
        $data = $natalReportData['data'];
        $text = $natalReportData['text'];
        $unlockRecodeInfo = [ // 解锁档案信息
            'recode_is_change' => false,
            'info' => (new Util)->getFortuneDetailRecodeInfo([$natalReportData['recode_info']]),
            'change_title' => '',
            'change_desc' => ''
        ];

        return $this->fetch('../../miniweb/view/natal_report/' . $this->type[$gid], [
            'is_show_nav' => 0,//是否展示导航栏
            'appType' => $appType,
            'height' => $height,
            'levels' => $levels,
            'text' => $text,
            'data' => $data,
            'oss' => $oss,
            'callback_url' => $astro_home_page . $callback_url . '?channel=' . $channel,
            'astro_home_page' => $astro_home_page . '?channel=' . $channel,
            'is_show_download_bar' => $this->isShowDownloadBar($channel), //是否展示下载条
            'site_id' => Env::get('miniapp.site_id'),
            'is_expand' => true,
            'unlock_recode_info' => $unlockRecodeInfo
        ]);
    }

    /**
     * 是否展示下载条
     * @param $channelId
     * @return int|mixed
     * @throws \Exception
     */
    private function isShowDownloadBar($channelId)
    {
        $channelInfo = (new MiniChannel())->getInfoById($channelId, ['is_show_download_bar']);
        $is_show_download_bar = 0;
        if ($channelInfo)
        {
            $is_show_download_bar = $channelInfo['is_show_download_bar'];
        }
        return (int)$is_show_download_bar;
    }
}
